/*
 * logtext.c
 *
 *  Created on: 2010-12-9
 *      Author: root
 */

#include "logtext.h"
#define BUFSIZE 2048

//写入文档日志
 void logtext(int handel, const u_char *buf)
{

	char buff[BUFSIZE];


	struct timeval tv_time;
	gettimeofday (&tv_time,NULL);
	struct tm *ct = localtime(&tv_time.tv_sec);
	//time_t t = time(NULL);
	//char *ct = ctime(&t);
	// ct[strlen(ct)-1] = ' ';

   struct tcphdr *tcph = (struct tcphdr *) buf;
          sprintf(buff,
        		           "[ %d-%d-%d  %d:%d:%d.%d ]"
        		           "\n TCP header "
            		     "\n Source port: %d"
                       "\n Destination port: %d"
                       "\n Sequence number: %u"
                       "\n Acknowledgment number: %u"
                       "\n Header length: %d (%d bytes)"
                       "\n Reserved: 0x%X"
                       "\n Flags: r:%d,u:%d,a:%d,p:%d,r:%d,s:%d,f:%d"
                       "\n Window size: %d"
                       "\n Checksum: 0x%02X%02X"
                       "\n Urgent pointer: %d"
                		  "\n"
               		  "\n"
        		   ,(ct->tm_year)+1900,ct->tm_mon,ct->tm_mday,ct->tm_hour,ct->tm_min,ct->tm_sec,tv_time.tv_usec
               ,ntohs(tcph->source)
               ,ntohs(tcph->dest)
               ,ntohl(tcph->seq)
               ,ntohl(tcph->ack_seq)
               ,tcph->doff, tcph->doff*4
               ,tcph->res1
               ,tcph->res2, tcph->urg, tcph->ack, tcph->psh,tcph->rst, tcph->syn, tcph->fin
               ,ntohs(tcph->window)
               ,PWORD(tcph->check)
               ,ntohs(tcph->urg_ptr));
         	    write(handel,buff,strlen(buff));
   	    //printf("/n,lanyulanyulanyulanyulanyu");

}

